package com.yc.sole.system.controller;

import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import com.yc.sole.system.biz.UserBiz;
import com.yc.sole.system.request.UserQueryReq;
import com.yc.sole.system.request.UserReq;
import com.yc.sole.system.request.UserUpdatePwdReq;
import com.yc.sole.system.response.UserResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户管理
 *
 * @author yizuomin
 * @date 2023-03-26
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserBiz userBiz;

    /**
     * 新增
     *
     * @param userReq
     * @return RestResult
     */
    @PreAuth(value = "system:user:save", description = "新增用户")
    @PostMapping("/api/system/user/saveUser")
    public Result<Void> saveUser(@RequestBody @Validated(SaveGroup.class) UserReq userReq) {
        userBiz.saveUser(userReq);
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param userReq
     * @return RestResult
     */
    @PreAuth(value = "system:user:update", description = "修改用户")
    @PostMapping("/api/system/user/updateUserById")
    public Result<Void> updateUserById(@RequestBody @Validated(UpdateGroup.class) UserReq userReq) {
        userBiz.updateUserById(userReq);
        return Result.success();
    }

    /**
     * 重制用户密码
     *
     * @param userId
     * @return RestResult
     */
    @PreAuth(value = "system:user:update", description = "重制用户密码")
    @PostMapping("/api/system/user/resetPassword/{userId}")
    public Result<Void> resetPassword(@PathVariable Long userId) {
        userBiz.resetPassword(userId);
        return Result.success();
    }


    /**
     * 通过id删除
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "system:user:delete", description = "删除用户")
    @PostMapping("/api/system/user/deleteUser")
    public Result<Void> deleteUser(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        userBiz.deleteUser(baseDeleteReq.getIds());
        return Result.success();
    }

    /**
     * 修改用户密码
     *
     * @param updatePwdReq
     * @return RestResult
     */
    @PreAuth(value = "system:user:update", description = "修改用户密码", check = false)
    @PostMapping("/api/system/user/updatePassword")
    public Result<Void> updatePassword(@RequestBody @Validated UserUpdatePwdReq updatePwdReq) {
        userBiz.updatePassword(updatePwdReq);
        return Result.success();
    }


    /**
     * 分页查询
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "system:user:select", description = "分页查询用户")
    @PostMapping("/api/system/user/selectUserPage")
    public Result<Paging<UserResp>> selectUserPage(@RequestBody @Validated BasePageReq<UserQueryReq> basePageReq) {
        return Result.data(userBiz.selectUserPage(basePageReq.toPage(), basePageReq.getQuery()));
    }

    /**
     * 根据id查询用户
     *
     * @param userId
     * @return Result
     */
    @PreAuth(value = "system:user:select", description = "根据id查询用户")
    @PostMapping("/api/system/user/selectUserById/{userId}")
    public Result<UserResp> selectUserById(@PathVariable Long userId) {
        return Result.data(userBiz.selectUserById(userId));
    }
}
